<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row>
        <el-col> <el-button type="primary" @click="addCate">添加分类按钮</el-button></el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table :data="categoriesList" :columns="columns" :expand-type="false" :selection-type="false" :show-index="true" index-text="#" border :show-row-hover="false" class="treetable">
        <!-- 是否有效的模板 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color:lightgreen" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <!--排序  -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag v-if="scope.row.cat_level === 1" type="success" size="mini">二级</el-tag>
          <el-tag v-if="scope.row.cat_level === 2" type="warning" size="mini">三级</el-tag>
        </template>
        <!--操作  -->
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini">
            编辑
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">
            删除
          </el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="50%">
      <el-form :model="addRuleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="活动名称" prop="cat_name">
          <el-input v-model="addRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <div class="block">
            <!-- 绑定的数组 配置名称值  存储返回的选中的父级id 、
             文本一改变就触发的事件 可清空操作 -->
            <el-cascader :options="parentCateList" :props="propsParams" v-model="catekeys" @change="changeParentList" clearable @close="clsoeForm" ref="formRef"> </el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 总的分类列表
      categoriesList: [],
      // 分类的参数
      paramInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 总分类数据
      total: 0,
      // 为表格指定列的定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          // 将当前列定义为模板列
          type: "template",
          // 表示当前列使用的模板名称
          template: "isok"
        },
        {
          label: "排序",
          // 将当前列定义为模板列
          type: "template",
          // 表示当前列使用的模板名称
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ],
      // 控制添加分类的按钮
      addDialogVisible: false,
      // 添加分类的参数
      addRuleForm: {
        cat_name: "",
        // 分类父级id
        cat_pid: 0,
        // 分类父级等级
        cat_level: 0
      },
      // 下拉框分类的数据
      parentCateList: [],
      // 下拉框中选择的分类的id
      catekeys: [],
      // 下拉框的数据配置
      propsParams: {
        checkStrictly: "true", //可只选择一层的级联
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        children: "children"
      },
      // 分类表单的验证规则
      rules: {
        cat_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getCategories()
  },
  methods: {
    // 获取分类信息列表
    async getCategories() {
      const { data: res } = await this.$http.get("categories", {
        params: this.paramInfo
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error("获取分类列表失败")
      this.categoriesList = res.data.result
      this.total = res.data.total
    },
    // 分页区域
    handleSizeChange(newSize) {
      this.paramInfo.pagesize = newSize
      this.getCategories()
    },
    handleCurrentChange(newNum) {
      this.paramInfo.pagenum = newNum
      this.getCategories()
    },
    // 添加分类按钮
    addCate() {
      this.getParentCateList()
      this.addDialogVisible = true
    },
    // 获取父级分类的数据列表 前两个父级
    async getParentCateList() {
      var { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      })
      console.log(res.data)
      if (res.meta.status !== 200) return this.$message.error("获取分类数据列表失败")
      this.parentCateList = res.data
    },
    // 当分类选择器一改变的时候触发
    changeParentList() {
      console.log(this.catekeys)
      if (this.catekeys.length > 0) {
        this.addRuleForm.cat_pid = this.catekeys[this.catekeys.length - 1]
        this.addRuleForm.cat_level = this.catekeys.length
        return
      } else {
        this.addRuleForm.cat_pid = 0
        this.addRuleForm.cat_level = 0
        return
      }
    },
    // 添加分类的确定按钮
    async submitAdd() {
      var { data: res } = await this.$http.post("categories", this.addRuleForm)
      if (res.meta.status !== 201) return this.$message.error("添加失败")
      this.$message.success("添加失败")
      console.log(this.addRuleForm)
      this.addDialogVisible = false
    },
    // 当关闭表单的时候
    clsoeForm() {
      this.$refs.formRef.resetFields()
      this.catekeys = []
      this.addRuleForm.cat_pid = 0
      this.addRuleForm.cat_level = 0
    }
  }
}
</script>
<style scoped>
.treetable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
